package social.hub.oauth;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import social.hub.oauth.oauth4j.OAuth4JServiceProvider;
import social.hub.oauth.scribe.ScribeServiceProvider;

public class ITOAuthServiceProviderFactory {

	private OAuthServiceProviderFactory factory;
	private OAuthServiceConfig serviceConfig;

	@Before
	public void setUp() throws Exception {

		factory = OAuthServiceProviderFactory.getInstance();

		serviceConfig = new OAuthServiceConfig();

		serviceConfig.setRequestTokenUrl("http://api.twitter.com/oauth/request_token");
		serviceConfig.setAccessTokenUrl("http://api.twitter.com/oauth/access_token");
		serviceConfig
				.setAuthorizeUrl("https://api.twitter.com/oauth/authorize?oauth_token={requestToken}");

		serviceConfig.setApiKey("FHWExNfm2mvTHfIErdKg");
		serviceConfig.setApiSecret("mWKacnA3wfrNJTCRC3AQvLzxJk1C18fzoIdH68lspU");
		serviceConfig.setCallbackUrl("https://www.teste.com.br/requestCallback");

	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetServiceProvider() {

		OAuthServiceProvider serviceProvider = factory.getServiceProvider(serviceConfig);

		assertNotNull(serviceProvider);
		assertSame(serviceConfig, serviceProvider.getServiceConfig());

	}

	@Test
	public void testGetServiceProvider_OAuth4j() {

		OAuthServiceProvider serviceProvider = factory.getServiceProvider(serviceConfig,
				OAuthProviderType.OAUTH4J);

		assertNotNull(serviceProvider);
		assertSame(serviceConfig, serviceProvider.getServiceConfig());
		assertTrue(serviceProvider instanceof OAuth4JServiceProvider);

	}

	@Test
	public void testGetServiceProvider_Scribe() {

		OAuthServiceProvider serviceProvider = factory.getServiceProvider(serviceConfig,
				OAuthProviderType.SCRIBE);

		assertNotNull(serviceProvider);
		assertSame(serviceConfig, serviceProvider.getServiceConfig());
		assertTrue(serviceProvider instanceof ScribeServiceProvider);

	}

	@Test
	public void testSetDefaultType_OAuth4j() {

		factory.setDefaultType(OAuthProviderType.OAUTH4J);

		OAuthServiceProvider serviceProvider = factory.getServiceProvider(serviceConfig);

		assertNotNull(serviceProvider);
		assertSame(serviceConfig, serviceProvider.getServiceConfig());
		assertTrue(serviceProvider instanceof OAuth4JServiceProvider);

	}

	@Test
	public void testSetDefaultType_Scribe() {

		factory.setDefaultType(OAuthProviderType.SCRIBE);

		OAuthServiceProvider serviceProvider = factory.getServiceProvider(serviceConfig);

		assertNotNull(serviceProvider);
		assertSame(serviceConfig, serviceProvider.getServiceConfig());
		assertTrue(serviceProvider instanceof ScribeServiceProvider);

	}

}
